package com.smartwasser.yunhtgl.apphtgl.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.App;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.bean.CaseProcess1Bean.ListBean;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import java.util.List;

import static com.smartwasser.yunhtgl.apphtgl.R.id.checkbox_case_layout;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.removeValue;

public class CaseAdapter01 extends BaseAdapter {
    private Context context;
    private List<ListBean> data;
    private Boolean checkVisibity = false;
    private Boolean checkAll = false;

    public CaseAdapter01(Context context, List<ListBean> list) {
        this.context = context;
        this.data = list;
    }

    public interface OnItemCheckListener {

        void onItemcheck(int position);
    }

    private OnItemCheckListener onItemClickListener;

    public void setOnCheckListener(OnItemCheckListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void refresh(List<ListBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    public void checkAll(Boolean boom) {
        this.checkAll = boom;
        if (boom) {
            for (int i = 0; i < data.size(); i++) {
                App.getInstance().setCaseProcess1List(data.get(i).getId());
            }
        }
    }

    public void chechVisibity(Boolean boom) {
        this.checkVisibity = boom;
    }

    @Override
    public int getCount() {
        if (null != data && data.size() > 0) {
            return data.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolders holders;
        if (convertView == null) {
            holders = new ViewHolders();
            convertView = View.inflate(context, R.layout.case_item1, null);
            holders.checkbox_case_layout = convertView.findViewById(checkbox_case_layout);
            holders.checkbox_case = convertView.findViewById(R.id.checkbox_case);
            holders.sp_item_name = convertView.findViewById(R.id.sp_item_name);
            holders.sp_item_bd = convertView.findViewById(R.id.sp_item_bd);
            holders.sp_item_money = convertView.findViewById(R.id.sp_item_money);
            holders.sp_item_id = convertView.findViewById(R.id.sp_item_id);
            holders.sp_item_type = convertView.findViewById(R.id.sp_item_type);
            convertView.setTag(holders);
        } else {
            holders = (ViewHolders) convertView.getTag();
        }
        if (null != data && data.size() > 0) {
            Logger.e("data.size = " + data.size());
            if (onItemClickListener != null) {
                holders.checkbox_case_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (holders.checkbox_case.isChecked()) {
                            removeValue(App.getInstance().getCaseProcess1List(), data.get(position).getId());
                        } else {
                            App.getInstance().setCaseProcess1List(data.get(position).getId());
                        }
                        onItemClickListener.onItemcheck(position);
                    }
                });
            }

            if (checkVisibity) {
                holders.checkbox_case.setVisibility(View.VISIBLE);
                if (checkAll && !(App.getInstance().getCaseProcess1List().contains(data.get(position).getId()))) {
                    App.getInstance().setCaseProcess1List(data.get(position).getId());
                }
                if (App.getInstance().getCaseProcess1List().contains(data.get(position).getId())) {
                    holders.checkbox_case.setChecked(true);
                } else {
                    holders.checkbox_case.setChecked(false);
                }
            } else {
                holders.checkbox_case.setVisibility(View.GONE);
            }
            holders.sp_item_name.setText(data.get(position).getName());
            holders.sp_item_bd.setText("标的 : " + data.get(position).getContractObject());
            holders.sp_item_money.setText("金额 : " + data.get(position).getContractValue());
            holders.sp_item_id.setText("编号 : " + data.get(position).getCode());
            holders.sp_item_type.setText("类型 : " + data.get(position).getType());
        }
        return convertView;
    }

    class ViewHolders {
        public LinearLayout checkbox_case_layout;
        public CheckBox checkbox_case;
        public TextView sp_item_name;
        public TextView sp_item_bd;
        public TextView sp_item_money;
        public TextView sp_item_id;
        public TextView sp_item_type;
    }
}